<template>
  <div class="minLogin">
    <a-spin :spinning="loading" style="background: #fff; border-radius: 3%; padding: 20px 30px; 10px 30px">
      <a-form id="formLogin" class="user-layout-login" ref="formLogin" :form="form" @submit="handleSubmit">
        <a-tabs
          :activeKey="customActiveKey"
          :tabBarStyle="{ textAlign: 'center', borderBottom: 'unset' }"
          @change="handleTabClick"
          style="height: 200px"
        >
          <a-tab-pane key="tab1" tab="账号密码登录">
            <a-form-item>
              <a-input
                size="large"
                type="text"
                placeholder="请输入手机号"
                v-decorator="['userName', { rules: [{ required: true, message: '请输入手机号' }] }]"
              >
                <a-icon slot="prefix" type="user" :style="{ color: 'rgba(0,0,0,.25)' }" />
              </a-input>
            </a-form-item>

            <a-form-item>
              <div style="display: flex">
                <div style="flex: 7">
                  <a-input
                    size="large"
                    type="text"
                    autocomplete="false"
                    placeholder="请输入验证码"
                    v-decorator="['SmsCode', { rules: [{ required: false, message: '请输入验证码' }] }]"
                  >
                    <a-icon slot="prefix" type="woman" :style="{ color: 'rgba(0,0,0,.25)' }" />
                  </a-input>
                </div>

                <div style="flex: 3; margin-left: 5px">
                  <a-button
                    class="button_obtain"
                    v-show="sendSmsCode"
                    @click.prevent="codeChange"
                    style="outline: none; height: 40px"
                    type="primary"
                  >
                    获取验证码
                  </a-button>
                  <span v-show="!sendSmsCode" class="auth_text">
                    <span class="auth_text_blue">{{ auth_time }}</span
                    >秒之后重发</span
                  >
                </div>
              </div>
            </a-form-item>
          </a-tab-pane>
        </a-tabs>
        <a-form-item>
          <a-button size="large" type="primary" htmlType="submit" class="login-button">确定</a-button>
        </a-form-item>
      </a-form>
    </a-spin>
  </div>
</template>

<script>
import TokenCache from '@/utils/cache/TokenCache'
const reg = /^1[345789]\d{9}$/ // 手机号正则
export default {
  data() {
    return {
      sendSmsCode: true, //显示‘获取按钮'还是'倒计时'
      auth_time: 0 /*倒计时 计数器*/,
      loading: false,
      customActiveKey: 'tab1',
      form: this.$form.createForm(this),
      required: false,
    }
  },
  mounted() {
    var userName = localStorage.getItem('userName')
    var password = localStorage.getItem('password')
    if (userName && password) {
      this.form.setFieldsValue({ userName, password, savePwd: true })
    }
  },
  methods: {
    // 发送验证码
    codeChange() {
      const values = this.form.getFieldsValue()
      console.log('开始发送验证码了', values)
      this.form.validateFields((errors, values) => {
        // 校验成功
        if (!errors) {
          values = this.form.getFieldsValue()
          // 校验手机号格式
          if (!reg.test(values.userName)) return this.$message.error('请核对手机号格式')
          this.loading = true
          this.$http.post('/Base_Manage/Base_User/GenerateVerificationCode?Phone=' + values.userName).then((res) => {
            if (res.Success) {
              this.$message.success('短信发送成功')
              // 显示正在发送
              this.sendSmsCode = false
              //设置倒计时秒
              this.auth_time = 60
              var auth_timetimer = setInterval(() => {
                this.auth_time--
                if (this.auth_time <= 0) {
                  this.sendSmsCode = true
                  clearInterval(auth_timetimer)
                }
              }, 1000)
              this.loading = false
            } else {
              this.loading = false
              this.$message.error(res.Msg)
            }
          })
        }
      })
    },
    handleTabClick(key) {
      this.customActiveKey = key
      // this.form.resetFields()
    },
    handleSubmit(e) {
      e.preventDefault()
      this.form.validateFields((errors, values) => {
        // 校验成功
        if (!errors) {
          values = this.form.getFieldsValue()
          // 校验手机号格式
          if (!reg.test(values.userName)) return this.$message.error('请核对手机号格式')
          // 判断验证码
          if (!values.SmsCode) return this.$message.error('请输入验证码')
          this.loading = true
          this.$http.post('/Base_Manage/Home/SubmitLogin', values).then((resJson) => {
            this.loading = false
            if (resJson.Success) {
              TokenCache.setToken(resJson.Data)
              // 保存密码
              if (values['savePwd']) {
                localStorage.setItem('userName', values['userName'])
                localStorage.setItem('password', values['password'])
              } else {
                localStorage.removeItem('userName')
                localStorage.removeItem('password')
              }
              location.reload()
              this.$router.push({ path: '/' })
            } else {
              this.$message.error(resJson.Msg)
            }
          })
        }
      })
    },
  },
}
</script>

<style lang="less" scoped>
.minLogin {
  margin: 20px auto 50px;
  width: 368px;
  text-align: left;
}

.user-layout-login {
  label {
    font-size: 14px;
  }

  .getCaptcha {
    display: block;
    width: 100%;
    height: 40px;
  }

  .forge-password {
    font-size: 14px;
  }

  button.login-button {
    padding: 0 15px;
    font-size: 16px;
    height: 40px;
    width: 100%;
  }

  .user-login-other {
    text-align: left;
    margin-top: 24px;
    line-height: 22px;

    .item-icon {
      font-size: 24px;
      color: rgba(0, 0, 0, 0.2);
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: #1890ff;
      }
    }

    .register {
      float: right;
    }
  }
}
</style>
